﻿using System;
using System.Configuration;
using System.Web;

namespace Songhay.Web.Mvc.Extensions
{
    using Songhay.Xml;

    /// <summary>
    /// Static members for XSL processing in MVC context.
    /// </summary>
    public static partial class XslUtility
    {
        /// <summary>
        /// Loads an XSL transformation based on conventional Application settings
        /// and the specified XML string.
        /// </summary>
        /// <param name="applicationSetting">The Application Setting name.</param>
        /// <param name="xsltCommand">The XSLT command.</param>
        /// <param name="xmlString">The XML string.</param>
        /// <returns>Returns the <see cref="System.String"/> results of the XSL transformation.</returns>
        public static string XslTransform(string applicationSetting, string xsltCommand, string xmlString)
        {
            string s = null;
            string serverPath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings[applicationSetting]);
            s = XmlUtility.LoadXslTransform(serverPath, xsltCommand, xmlString);
            s = XmlUtility.StripNamespaces(s);
            s = Environment.ExpandEnvironmentVariables(s);

            return s;
        }

        /// <summary>
        /// Returns the transformed Server Index data based on conventional Application settings.
        /// </summary>
        /// <param name="applicationSettingXml">The Application Setting name for the XML file.</param>
        /// <param name="applicationSettingXsl">The Application Setting name for the XSLT file.</param>
        /// <returns>Returns the <see cref="System.String"/> results of the XSL transformation.</returns>
        public static string XslTransform(string applicationSettingXsl, string applicationSettingXml)
        {
            string s = null;

            s = XmlUtility.LoadXslTransform(
                HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings[applicationSettingXsl]),
                HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings[applicationSettingXml]));

            s = XmlUtility.StripNamespaces(s);
            s = Environment.ExpandEnvironmentVariables(s);

            return s;
        }

        /// <summary>
        /// Returns the transformed Server Index data based on the conventional message format.
        /// </summary>
        /// <param name="exceptionMessage">The message/data from the exception.</param>
        /// <returns>Returns the <see cref="System.String"/> results of the XSL transformation.</returns>
        public static string XslTransformExceptionMessage(string exceptionMessage)
        {
            var context = HttpContext.Current;

            string s = XmlUtility.GetInternalMessage(
                "Exception",
                new string[] { exceptionMessage,
                        string.Format("HTTP Method: {0}", context.Request.HttpMethod.ToString()),
                        string.Format("HTTP Headers: {0}", XmlUtility.ExpandSpecialChars(context.Request.Headers.ToString())),
                        string.Format("Request Parameters: {0}", XmlUtility.ExpandSpecialChars(context.Request.Params.ToString())),
                        string.Format("Request Type: {0}", context.Request.RequestType.ToString()),
                        });

            return s;
        }
    }
}
